<?php
namespace app\admin\controller;

use app\common\model\UsedConsult as UsedConsultModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 管理
 * Class Adminusedconsult
 * @package app\admin\controller
 */
class UsedConsult extends AdminBase
{
    protected $usedconsult_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->usedconsult_model = new UsedConsultModel();
    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['name|phone'] = ['like', "%{$keyword}%"];
        }
        $usedconsult_list = $this->usedconsult_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('index', ['usedconsult_list' => $usedconsult_list, 'keyword' => $keyword]);
    }


    /**
     * 查看
     * @param $id
     * @return mixed
     */
    public function reading($id)
    {
        $usedconsult = $this->usedconsult_model->find($id);

        return $this->fetch('reading', ['usedconsult' => $usedconsult]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();

            $data['manage'] == 1;
            if ($this->usedconsult_model->allowField(true)->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;
        if($id){
            if ($this->usedconsult_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }else{
            $this->error('请选择需要删除的内容');
        }
        
    }

}